package yl.hs.bmipfull.controllers.home;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.system.WechatUserEntity;
import yl.hs.bmipfull.service.salemanage.ILinkerInfoService;
import yl.hs.bmipfull.service.system.IWechatUserService;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * @author 谢东
 * @apiNote 远程下单——工地二维码下单相关操作控制层
 * @since 2022/8/18 9:48 创建
 */
@Api(value = "工地二维码下单相关操作控制层", tags = "远程下单——工地二维码下单相关操作控制层")
@RequestMapping("/RemoteOrder")
@RestController
public class RemoteOrderController extends ControllerBase {
    @Autowired
    IWechatUserService wechatUserService;
    @Autowired
    ILinkerInfoService iLinkerInfoService;


    @ApiOperation(value = "根据微信用户openid获取注册信息", notes = "根据微信用户openid获取注册信息")
    @RequestMapping(value = "/byopenid/{openid}", method = RequestMethod.GET)
    public WebResult findWeCharUser(@PathVariable String openid) {
        var user = wechatUserService.findByOpenId(openid);
        if (user != null) {
            var map = user.toMap();
            var linker = iLinkerInfoService.findByOpenId(openid);
            if (linker != null) {
                map.put("linker", linker);
            }
            return success("查询成功", map);
        } else {
            return success("未找到注册信息");
        }
    }

    @ApiOperation(value = "保存微信注册用户信息", notes = "查询/更新微信注册用户信息信息")
    @RequestMapping(value = {"/save","/save"}, method = RequestMethod.PUT)
    public WebResult saveWechatUser(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            var map =submitModel.getData();
            submitModel.getData().remove("id");
            result = wechatUserService.saveProjectWechatUser(submitModel.getData(), Long.valueOf(map.get("id").toString()));
            if (result > 0) {
                return success("保存成功");
            } else if (result==-2){
                return fail("未查询到用户信息");
            }else {
                return fail("数据未保存");
            }

        } catch (Exception e) {
            return error(e);
        }

    }
}
